package test.yzx.pproject.act2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import test.yzx.pproject.R;
import test.yzx.pproject.util.StickyExpandableListAdapter;
import test.yzx.pproject.util.StickyExpandableListLayout;

public class ExpendableListViewActivity extends AppCompatActivity{

    private StickyExpandableListLayout lv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_lv);

        lv = (StickyExpandableListLayout) findViewById(R.id.lv);

        View hv = new View(this);
        hv.setLayoutParams(new ExpandableListView.LayoutParams(-1, 150));
        lv.getReal().addHeaderView(hv, null, false);

        final Adapter adapter = new Adapter();
        lv.getReal().setAdapter(adapter);
        lv.getReal().expandAllGroup();

    }


    private class Adapter extends StickyExpandableListAdapter{
        @Override
        public View getCopyGroupView(final int groupPosition) {
            View header = getLayoutInflater().inflate(R.layout.exp_group, null, false);
            ((TextView)header.findViewById(R.id.tv)).setText(""+groupPosition);
            header.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    lv.getReal().toggle(groupPosition, false);
                }
            });
            return header;
        }
        @Override
        public int getGroupCount() {
            return 5;
        }
        @Override
        public int getChildrenCount(int groupPosition) {
            return 10;
        }
        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if(convertView==null)
                convertView = getLayoutInflater().inflate(R.layout.exp_group, parent, false);
            ((TextView)convertView.findViewById(R.id.tv)).setText(""+groupPosition);
            convertView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    lv.getReal().toggle(groupPosition, false);
                }
            });
            return convertView;
        }
        @Override
        public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if(convertView==null)
                convertView = getLayoutInflater().inflate(R.layout.exp_chid, parent, false);
            return convertView;
        }
    }

}
